<template>
  <div>
    <div class="content">
      <div class="myNavCss">
        <myNav :thisNav="'0'" />
      </div>
      <div class="myInfo">
        <div class="myInfo-left">
          <p class="font16 cl333 bold font14 mt10">基本信息</p>
          <div class="headerSex">
            <div class="header">
              <img :src="userData.avatar" class="pic_mine" id="avatar" >
              <input style="  cursor: pointer;" type="file" class="avatarFile" accept="image/*" @change="handleFileUpload">
            </div>
            <el-radio-group v-model="userData.sex">
              <el-radio :label="1">男</el-radio>
              <el-radio :label="2">女</el-radio>
            </el-radio-group>
          </div>
          <div style="margin-top: 25px">
            <label class="form-label">姓名</label>
            <el-input v-model="userData.nickname" />
          </div>
          <div>
            <label class="form-label">所属行业</label>
            <el-select v-model="userData.industry">
              <el-option label="乙方综合性服务商" value="乙方综合性服务商" />
              <el-option label="广告媒体代理商" value="广告媒体代理商" />
              <el-option label="代运营服务商" value="代运营服务商" />
              <el-option label="工具类服务商" value="工具类服务商" />
              <el-option label="媒体方" value="媒体方" />
              <el-option label="甲方企业主" value="甲方企业主" />
            </el-select>
          </div>
          <p class="font16 cl333 bold font14" style="margin-top: 15px">联系方式</p>
          <div style="margin-top: 15px;display: flex;align-items: center">
            <em class="icon-form-phone"></em>
            <el-input v-model="userData.remarkMobile" />
          </div>
          <div style="margin-top: 15px;display: flex;align-items: center">
            <em class="icon-form-watch"></em>
            <el-input v-model="userData.wechatNum" />
          </div>
          <p class="font16 cl333 bold font14 mt30">职业信息</p>
          <div>
            <label class="form-label">公司 </label>
            <el-input placeholder="公司名称" v-model="userData.companyName"  />
          </div>
          <div>
            <label class="form-label">职位 </label>
            <el-input placeholder="工作职位" v-model="userData.jobTitle" />
          </div>
          <div>
            <label class="form-label">提供 <span style="color: red">*字数不超过120个</span></label>
            <el-input placeholder="一句话介绍你提供的服务" v-model="userData.provide" maxlength="120" />
          </div>
        </div>
        <div class="myInfo-right">
          <p class="font16 cl333 bold font14 mt10"></p>
          <div class="">
            <label class="form-label">账号信息</label>
            <p>
              <span class="font12 cl999" style="cursor: pointer" @click="binding(userData.isBindMobile)">
                <img src="@/assets/image/mobile_icon.png" class="common_icon" v-if="!userData.isBindMobile">
                {{ userData.isBindMobile ? userData.mobile : '未绑定' }}
              </span>
              <span class="ml8 font12 cl999" style="cursor: pointer" @click="bindWechat(userData.isBindMnp)">
                <img src="@/assets/image/wechat.png" class="common_icon">
                {{ userData.isBindMnp ? '已绑定' : '未绑定' }}
              </span>
            </p>
          </div>
          <div style="margin-bottom: 45px">
            <label class="form-label" style="display: flex;margin-bottom: 10px;">认证情况</label>
            <span class="font12 w_renzheng">未认证</span>
            <a href="javascript:void(0)" @click="lookEquities" class="renzheng_link">认证权益
              &gt;</a>
          </div>
          <div class="address">
            <div class="address-one">
              <label class="form-label">所在省份</label>
              <el-select v-model="userData.province" @change="selectProvince">
                <template v-for="item in area">
                  <el-option :label="item.name" :value="item.name"></el-option>
                </template>
              </el-select>
            </div>
            <div class="address-one">
              <label class="form-label">所在城市</label>
              <el-select v-model="userData.city">
                <template v-for="item in area[areaIndex].children">
                  <el-option :label="item.name" :value="item.name"></el-option>
                </template>
              </el-select>
            </div>
          </div>
          <div>
            <label class="form-label">业务领域</label>
            <el-input v-model="userData.businessArea" />
          </div>
          <p class="font16 cl333 bold font14 mt10"></p>
          <div style="margin-top:43px">
            <img src="https://www.uqudao.com/static/web/img/common/code_icon.png" class="common_icon top" style="width: 20px;margin-right: 10px;margin-top: 3px">
            <div style="display: inline-block;width: 110px;height: 110px;position: relative">
              <uploadimg :limit="1" @saveImg="addImgOne" :values="imgListOne" />
            </div>
            <span class="bottom ml8 font12 cl999">微信二维码</span>
          </div>
          <div style="margin-top: 28px">
            <label class="form-label">品牌</label>
            <el-input placeholder="品牌名称" v-model="userData.brand" />
          </div>
          <div style="margin-top: 64px">
            <label class="form-label">需要 <span style="color: red">*字数不超过120个</span></label>
            <el-input placeholder="一句话介绍你需要的服务" v-model="userData.need" maxlength="120" />
          </div>
        </div>
        <div style="width: 100%">
          <label class="form-label">公司介绍</label>
          <el-input placeholder="编辑你们公司的介绍（内容不能包含联系方式）" type="textarea" :rows="5" v-model="userData.companyInfo" />
        </div>
        <div>
          <label class="form-label" style="display: flex">相关图片</label>
          <uploadimg :acceptText="'(最多9张，图中不能带有联系方式)'" :values="imgList" :limit="9" @saveImg="addImg($event,'imageList')" />
        </div>
        <div style="display: block;width: 100%;text-align: center;margin-top: 15px">
          <el-button type="primary" round style="width: 40%" :loading="LoadingBool" @click="submitFun">保存修改</el-button>
        </div>
      </div>
    </div>

    <bindingDom :dialogTableVisible="dialogTableVisible" @cancelLogin="cancelLoginFun" />

    <login :dialogTableVisible="LoginDialogTableVisible" @cancelLogin="cancelLoginFun" ref="childRef" :redirect="'https://www.qchuhai.com/#/'" />

  </div>
</template>
<script setup lang="ts">
import myNav from '@/components/myNav/index.vue'
import { ref } from "vue";
import {userInfoApi, userInfoEdit, uploadImage, logout, unbindWechat} from "@/api/api";
import uploadimg from "@/components/uploadImg/index.vue";
import bindingDom from './components/binding.vue'
import {ElMessage, ElMessageBox} from "element-plus";
import area from "@/common/area.json"
import {toRefs} from "vue";
import {useSysStore} from "@/store/user";
import Login from "@/layout/components/login.vue";
const { userInfo } = toRefs(useSysStore());
const userData = ref({
  sex: '0',
  mobile: '',
  isBindMnp: false,
  isBindMobile: false,
  remarkMobile: '',
  avatar: '',
  nickname: '',
  wechatNum: '',
  businessArea: '',
  need: '',
  provide: '',
  companyInfo: '',
  industry: '',
  imageList: [],
  wechatQrcode: '',
  province: '',
  city: '',
  brand: '',
  companyName: '',
  jobTitle: ''
})
interface imgIn{
  url: string
}
const dialogTableVisible = ref(false)
const LoginDialogTableVisible = ref(false)
const imgList = ref<imgIn[]>([])
const imgListOne = ref<imgIn[]>([])
const areaIndex = ref(0)
const LoadingBool = ref(false)
const addImg = (data:any, name:any) => {
  userData.value[name] = data.map((item:any) => {return item.url})
}
const addImgOne = (data:any) => {
  console.log(data)
  if (data.length > 0) {
    userData.value.wechatQrcode = data[0].url
  } else {
    userData.value.wechatQrcode = ''
  }

}
const handleFileUpload = (event:any) => {
  let file = event.target.files[0];
  let formData = new FormData()
  formData.append('file', file)
  uploadImage(formData).then((res:any) => {
    if (res.code === 200) {
      userData.value.avatar = res.data.path
    }
  })
}

const getUserInfo = () => {
  userInfoApi().then((res:any) => {
    userInfo.value = JSON.stringify(res.data)
    localStorage.setItem('userInfo', JSON.stringify(res.data))
    for (const i in userData.value) {
      userData.value[i] = res.data[i]
    }
    imgListOne.value = []
    if (res.data.wechatQrcode !== '') {
      imgListOne.value[0] = {url: res.data.wechatQrcode}
    }
    imgList.value = []
    if (userData.value.imageList.length > 0) {
      userData.value.imageList.forEach((item:any) => {
        imgList.value.push({
          url: item
        })
      })
    }
    if (res.data.province !== '') {
      areaIndex.value = area.findIndex((item:any) => item.name === res.data.province)
    } else {
      areaIndex.value = 0
    }
  })
}
getUserInfo()
const selectProvince = () => {
  userData.value.city = ''
  areaIndex.value = area.findIndex((item:any) => item.name === userData.value.province)
}
const submitFun = () => {
  LoadingBool.value = true
  userInfoEdit(userData.value).then((res:any) => {
    LoadingBool.value = false
    if (res.code === 200) {
      ElMessage.success('修改成功')
      getUserInfo()
    } else {
      ElMessage.warning(res.msg)
    }
  }).catch((err:any) =>{
    console.log(err)
    LoadingBool.value = false
  })
}

const lookEquities = () => {
  ElMessage.warning("暂未开放")
}
const binding = (value:Boolean) => {
  if (!value) {
    dialogTableVisible.value = true
  }
}

const bindWechat = (value:Boolean) => {
  if (value){
    ElMessageBox.confirm('确定要解绑微信？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      unbindWechat().then((res:any) => {
        if (res.code === 200) {
          ElMessage.success('解绑成功')
          getUserInfo()
        } else {
          ElMessage.error(res.msg)
        }
      })
      // 确定按钮被点击的回调
    }).catch(() => {
      // 取消按钮被点击的回调
      console.log('取消按钮被点击');
    });
  }else{
    LoginDialogTableVisible.value = true
  }

}


const cancelLoginFun = () => {
  dialogTableVisible.value = false
  LoginDialogTableVisible.value = false
  getUserInfo()
}
</script>
<style scoped>
.wechat_code{
  opacity: 0;
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
}
.address{
  display: flex;
  justify-content: space-between;
}
.address-one{
  width: 49%;
}
.icon-form-phone{
  display: inline-block;
  width: 16px;
  height: 16px;
  background: url("@/assets/image/mobile_icon.png") no-repeat;
  background-size: 100%;
  margin-right: 10px;
}
.w_renzheng {
  color: #fff;
  padding: 4px 8px;
  margin-right: 13px;
  border-radius: 11px;
  background: #c2c2c2!important;
  top: 0!important;
}
.renzheng_link {
  color: #409eff;
  text-decoration: underline;
  font-size: 12px;
}
.icon-form-watch{
  display: inline-block;
  width: 16px;
  height: 16px;
  background: url("@/assets/image/wechat_icon.png") no-repeat;
  background-size: 100%;
  margin-right: 10px;
}
.content{
   padding: 30px 0;
   position: relative;
   overflow: hidden;
   width: 1200px;
   margin: auto;
   box-sizing: border-box;
   display: flex;
 }
.myNavCss{
  width: calc(32% - 22px);
}
.font12 {
  font-size: 12px;
}
.cl999 {
  color: #999;
}
.ml8 {
   margin-left: 8px;
}
.common_icon {
  width: 14px;
  display: inline-block;
  vertical-align: middle;
  position: relative;
  top: -1px;
}
.myInfo{
  width: 68%;
  background: #fff;
  margin-left: 22px;
  padding: 20px 30px;
  display: flex;
  flex-wrap: wrap;
}
.myInfo-left{
  width: 48%;
  margin-right: 20px;
}
.myInfo-right{
  width: calc(52% - 20px);
}
.mt10{
  margin-top:10px;
}
.bold {
  font-weight: bold;
}
.font16 {
  font-size: 16px;
}
.cl333 {
  color: #333;
}
.headerSex{
  display: flex;
  align-items: center;
  margin-top: 15px;
}
.header{
  width: 90px;
  height: 90px;
  margin-right: 25px;
  position: relative;
}
.header input{
  position: absolute;
  top: 0;
  height: 100%;
  width: 100%;
  border-radius: 50%;
  opacity: 0;
}
.pic_mine {
  width: 90px;
  height: 90px;
  border-radius: 50%;
  border: 1px solid #ddd;
}
.form-label{
  color: #333;
  font-size: 12px;
  margin-top: 10px;
  margin-bottom: 5px;
  display: inline-block;
}
:deep(.el-input__wrapper),:deep(.el-select__wrapper), :deep(.el-textarea__inner) {
  background-color: #f5f5f5 !important;
}
.mt30 {
  margin-top: 30px;
}
.top {
  vertical-align: top;
  display: inline-block;
}
.bottom {
  vertical-align: bottom;
  display: inline-block;
}
.add_pic {
  width: 100px;
  display: inline-block;
}
</style>